﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerfumeShopManagement.DAL;
using PerfumeShopManagement.Model;
using PerfumeShopManagement.Utility;
using PerfumeShopManagement.BLL;

namespace PerfumeShopManagement.View
{
    public partial class frmExportCheckManagement : Form
    {
        private BllExportCheck bllExportCheck = new BllExportCheck();
        private BllAccount bllAccount = new BllAccount();
        private frmExportCheck detailView;

        public frmExportCheckManagement()
        {
            InitializeComponent();
        }

        private void showDetailView(int id)
        {
            string status = "";
            ExportCheck exportCheck = bllExportCheck.Get(id);
            if (exportCheck != null)
                status = exportCheck.Status;

            if (status == Constants.NEW_STATUS
                || status == Constants.PENDING_STATUS
                || id == int.MaxValue)
            {
                detailView = new frmExportCheck(id, bllExportCheck, bllAccount);
                detailView.Visible = true;
                detailView.FormClosed += new FormClosedEventHandler(detailView_FormClosed);
                this.Enabled = false;
            }
            else
            {
                MessageBox.Show("Phiếu xuất phải ở trạng thái \"" 
                    + Constants.NEW_STATUS 
                    + "\" hoặc \"" 
                    + Constants.PENDING_STATUS 
                    + "\"");
            }
        }

        private void detailView_FormClosed(object sender, FormClosedEventArgs e)
        {
            reloadCustomerList();
            reloadGridView();
            reloadCustomerList();
            this.Enabled = true;
        }

        private void addItemForTotalCriteria()
        {
            cbbOperator.Items.Clear();
            cbbOperator.Items.Add(Constants.BIGGER_OR_EQUAL_COMPARISON); // >=
            cbbOperator.Items.Add(Constants.BIGGER_COMPARISON); // >
            cbbOperator.Items.Add(Constants.EQUAL_COMPARISON); // =
            cbbOperator.Items.Add(Constants.SMALLER_COMPARISON); // <
            cbbOperator.Items.Add(Constants.SMALLER_OF_EQUAL_COMPARISON); // <=
        }

        private void resetFormField()
        {
            txbExportCheckId.Text = "";
            dtpMakingDate.Value = DateTime.Now;
            cbbCustomer.Text = "";
            cbbOperator.Text = "";
            txbOperand.Text = "";
        }

        private void reloadCustomerList()
        {
            List<User> users = bllAccount.GetAll();
            cbbCustomer.Items.Clear();
            foreach (User user in users)
            {
                cbbCustomer.Items.Add(user.Name);
            }
        }

        private void reloadGridView()
        {
            // Load grid view
            object[] buffer = new object[5];
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            List<ExportCheck> exportChecks = bllExportCheck.GetAll();
            foreach (ExportCheck item in exportChecks)
            {
                buffer[0] = item.ExportCheckId;
                buffer[1] = item.Status;
                buffer[2] = item.ExportCheckTime;
                buffer[3] = item.User.Name;
                buffer[4] = item.Total;

                rows.Add(new DataGridViewRow());
                rows[rows.Count - 1].CreateCells(gridExportChecks, buffer);
            }
            gridExportChecks.Rows.Clear();
            gridExportChecks.Rows.AddRange(rows.ToArray());
        }

        private void frmExportCheckManagement_Load(object sender, EventArgs e)
        {
            addItemForTotalCriteria();
            resetFormField();
            reloadCustomerList();
            reloadGridView();
        }

        private void btnAddExportCheck_Click(object sender, EventArgs e)
        {
            showDetailView(int.MaxValue);
        }

        private void btnDeleteExportCheck_Click(object sender, EventArgs e)
        {
            if (gridExportChecks.SelectedRows.Count > 0)
            {
                int count = 0;
                foreach (DataGridViewRow item in gridExportChecks.SelectedRows)
                {
                    int id = (int)item.Cells["colExportCheckId"].Value;
                    if (bllExportCheck.Get(id).Status == Constants.NEW_STATUS
                        || bllExportCheck.Get(id).Status == Constants.PENDING_STATUS)
                    {
                        count++;
                    }
                }
                if (count < gridExportChecks.SelectedRows.Count)
                {
                    MessageBox.Show("Tất cả phiếu xuất được chọn phải ở trạng thái \""
                        + Constants.NEW_STATUS
                        + "\" hoặc \""
                        + Constants.PENDING_STATUS + "\"");

                    return;
                }

                bool allDeleted = true;
                foreach (DataGridViewRow item in gridExportChecks.SelectedRows)
                {
                    int id = (int)item.Cells["colExportCheckId"].Value;
                    if (!bllExportCheck.DeleteById(id))
                        allDeleted = false;
                }

                if (allDeleted)
                {
                    reloadGridView();
                    MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
                }
                else
                {
                    MessageBox.Show(Constants.FAILED_MESSAGE);
                }
            }
        }

        private void btnEditExportCheck_Click(object sender, EventArgs e)
        {
            if (gridExportChecks.SelectedRows.Count > 0)
            {
                DataGridViewRow item = gridExportChecks.SelectedRows[0];
                int id = (int)item.Cells["colExportCheckId"].Value;
                showDetailView(id);
            }
        }

        private void btnProcessAllNewOrders_Click(object sender, EventArgs e)
        {
            bllExportCheck.ProcessAllNewOrders();
            reloadGridView();
        }
    }
}
